//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "FdmArithmeticAverageCondition.h"
using namespace Cephei::QL::Experimental::Finitedifferences;
#include <gen/QL/Math/Array.h>
#include <gen/QL/Experimental/Finitedifferences/FdmMesher.h>
using namespace Cephei::QL::Math;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Experimental::Finitedifferences::CFdmArithmeticAverageCondition::CFdmArithmeticAverageCondition (Cephei::IVector<Double>^ averageTimes, Double fixedSum, UInt64 pastFixings, Cephei::QL::Experimental::Finitedifferences::IFdmMesher^ mesher, UInt64 equityDirection) 
{
	_pSpinlock = new boost::detail::spinlock ();
    CoVector<Double>^ _CaverageTimes;
    CFdmMesher^ _Cmesher;
    try
    {
#ifdef HANDLE
        _phFdmArithmeticAverageCondition = NULL;
#endif
        CoVector<Double>^ _CaverageTimes = safe_cast<CoVector<Double>^> (averageTimes);
        _CaverageTimes->Lock();
        INativeVector<Double>^ _NCIaverageTimes = _CaverageTimes->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCaverageTimes = safe_cast<CDoubleVector^>(_NCIaverageTimes);
        std::vector<QuantLib::Time>& _averageTimes = static_cast<std::vector<QuantLib::Time>&> (_NCaverageTimes->GetReference ());
        QuantLib::Real _fixedSum = (QuantLib::Real)ValueHelper::Convert (fixedSum);
        QuantLib::Size _pastFixings = (QuantLib::Size)ValueHelper::Convert (pastFixings);
        _Cmesher = safe_cast<CFdmMesher^> (mesher);
        _Cmesher->Lock();
        boost::shared_ptr<QuantLib::FdmMesher>& _mesher = static_cast<boost::shared_ptr<QuantLib::FdmMesher>&> (_Cmesher->GetShared ()); 
        QuantLib::Size _equityDirection = (QuantLib::Size)ValueHelper::Convert (equityDirection);
        _ppFdmArithmeticAverageCondition = new boost::shared_ptr<QuantLib::FdmArithmeticAverageCondition> (new QuantLib::FdmArithmeticAverageCondition ( _averageTimes,  _fixedSum,  _pastFixings,  _mesher,  _equityDirection ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_CaverageTimes != nullptr) _CaverageTimes->Unlock();
        if (_Cmesher != nullptr) _Cmesher->Unlock();
    }
}
Cephei::QL::Experimental::Finitedifferences::CFdmArithmeticAverageCondition::CFdmArithmeticAverageCondition (boost::shared_ptr<QuantLib::FdmArithmeticAverageCondition>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFdmArithmeticAverageCondition = NULL;
#endif
	_ppFdmArithmeticAverageCondition = &childNative;
    
}
Cephei::QL::Experimental::Finitedifferences::CFdmArithmeticAverageCondition::CFdmArithmeticAverageCondition (QuantLib::FdmArithmeticAverageCondition& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phFdmArithmeticAverageCondition = NULL;
#endif
	_ppFdmArithmeticAverageCondition = new boost::shared_ptr<QuantLib::FdmArithmeticAverageCondition> (&childNative);
    
    _FdmArithmeticAverageConditionOwner = owner;
    
}

Cephei::QL::Experimental::Finitedifferences::CFdmArithmeticAverageCondition::CFdmArithmeticAverageCondition (CFdmArithmeticAverageCondition^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFdmArithmeticAverageCondition = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppFdmArithmeticAverageCondition = new boost::shared_ptr<QuantLib::FdmArithmeticAverageCondition> (copy->GetShared());
        
    }
}
Cephei::QL::Experimental::Finitedifferences::CFdmArithmeticAverageCondition::CFdmArithmeticAverageCondition (System::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFdmArithmeticAverageCondition = NULL;
#endif
	if (!t->IsSubclassOf(CFdmArithmeticAverageCondition::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Experimental::Finitedifferences::CFdmArithmeticAverageCondition::CFdmArithmeticAverageCondition (QuantLib::Handle<QuantLib::FdmArithmeticAverageCondition>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phFdmArithmeticAverageCondition = &childNative;
	_ppFdmArithmeticAverageCondition = &static_cast<boost::shared_ptr<QuantLib::FdmArithmeticAverageCondition>>(childNative.currentLink());
    
    _FdmArithmeticAverageConditionOwner = owner;
}
Cephei::QL::Experimental::Finitedifferences::CFdmArithmeticAverageCondition::CFdmArithmeticAverageCondition (QuantLib::Handle<QuantLib::FdmArithmeticAverageCondition> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phFdmArithmeticAverageCondition = &childNative;
	_ppFdmArithmeticAverageCondition = &static_cast<boost::shared_ptr<QuantLib::FdmArithmeticAverageCondition>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Experimental::Finitedifferences::CFdmArithmeticAverageCondition::CFdmArithmeticAverageCondition (QuantLib::FdmArithmeticAverageCondition childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFdmArithmeticAverageCondition = NULL;
#endif
	_ppFdmArithmeticAverageCondition = new boost::shared_ptr<QuantLib::FdmArithmeticAverageCondition> (new QuantLib::FdmArithmeticAverageCondition (childNative));
    
}
#endif

Cephei::QL::Experimental::Finitedifferences::CFdmArithmeticAverageCondition::~CFdmArithmeticAverageCondition ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppFdmArithmeticAverageCondition != NULL)
    {
	    delete _ppFdmArithmeticAverageCondition;
        _ppFdmArithmeticAverageCondition = NULL;
    }
}
Cephei::QL::Experimental::Finitedifferences::CFdmArithmeticAverageCondition::!CFdmArithmeticAverageCondition ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppFdmArithmeticAverageCondition != NULL)
    {
	    delete _ppFdmArithmeticAverageCondition;
    }
}
QuantLib::FdmArithmeticAverageCondition& Cephei::QL::Experimental::Finitedifferences::CFdmArithmeticAverageCondition::GetReference ()
{
    if (_ppFdmArithmeticAverageCondition == NULL) throw gcnew NativeNullException ();
	return **_ppFdmArithmeticAverageCondition;
}
boost::shared_ptr<QuantLib::FdmArithmeticAverageCondition>& Cephei::QL::Experimental::Finitedifferences::CFdmArithmeticAverageCondition::GetShared ()
{
    if (_ppFdmArithmeticAverageCondition == NULL) throw gcnew NativeNullException ();
	return *_ppFdmArithmeticAverageCondition;
}
QuantLib::FdmArithmeticAverageCondition* Cephei::QL::Experimental::Finitedifferences::CFdmArithmeticAverageCondition::GetPointer ()
{
    if (_ppFdmArithmeticAverageCondition == NULL) throw gcnew NativeNullException ();
	return &**_ppFdmArithmeticAverageCondition;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::FdmArithmeticAverageCondition>& Cephei::QL::Experimental::Finitedifferences::CFdmArithmeticAverageCondition::GetHandle ()
{
	if (_phFdmArithmeticAverageCondition == NULL)
	{
		_phFdmArithmeticAverageCondition = new Handle<QuantLib::FdmArithmeticAverageCondition> (*_ppFdmArithmeticAverageCondition);
	}
	return *_phFdmArithmeticAverageCondition;
}
#endif
bool Cephei::QL::Experimental::Finitedifferences::CFdmArithmeticAverageCondition::HasNative () 
{
	return (_ppFdmArithmeticAverageCondition != NULL);
}

Cephei::QL::Experimental::Finitedifferences::IFdmArithmeticAverageCondition^ Cephei::QL::Experimental::Finitedifferences::CFdmArithmeticAverageCondition::ApplyTo (Cephei::QL::Math::IArray^ a, Double t)
{
    CArray^ _Ca;
    try
    {
        _Ca = safe_cast<CArray^> (a);
        _Ca->Lock();
        QuantLib::Array& _a = static_cast<QuantLib::Array&> (_Ca->GetReference ()); 
        QuantLib::Time _t = (QuantLib::Time)ValueHelper::Convert (t);
    	(*_ppFdmArithmeticAverageCondition)->applyTo ( _a,  _t );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Ca != nullptr) _Ca->Unlock();
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Experimental::Finitedifferences::IFdmArithmeticAverageCondition^ Cephei::QL::Experimental::Finitedifferences::CFdmArithmeticAverageCondition_Factory::Create (Cephei::IVector<Double>^ averageTimes, Double fixedSum, UInt64 pastFixings, Cephei::QL::Experimental::Finitedifferences::IFdmMesher^ mesher, UInt64 equityDirection)
{
    return gcnew CFdmArithmeticAverageCondition ( averageTimes,  fixedSum,  pastFixings,  mesher,  equityDirection);
}
